#pragma once

#include <iostream>
#include <unordered_map>

#include "Number.hpp"
#include "Identifier.h"

namespace cal
{

class VariableTable
{
public:
    using HashTable = std::unordered_map<Identifier, header::Number>;
    
public:
    VariableTable() :_hash_table{ } { }
    ~VariableTable() = default;

public:
    void           clear() { _hash_table.clear(); _added_id = {}; _added_val = {}; }
    bool           exist(const Identifier& name)     const;
    header::Number get_value(const Identifier& name) const;
    void           add(const Identifier& name, const header::Number& value);
    void           print(std::ostream& os = std::cout, int num_per_line = 4);
    Identifier     added_id()    const { return _added_id; }
    header::Number added_value() const { return _added_val; }
    std::string    added_assignment() const { return _added_id.str() + " = " + _added_val.str(); }
 
private:
    HashTable      _hash_table;
    Identifier     _added_id;     // 保存最新加入的 id
    header::Number _added_val;    // 保存左心加入的 val
};

}   // namespace cal
